// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2005-1-17

#ifndef __SCORE_WAR3ANALYZER_H_SERVER_SVR_MAIN__
#define __SCORE_WAR3ANALYZER_H_SERVER_SVR_MAIN__

#pragma once

#include <common/svr_base/base_pathstring.h>
#include <common/svr_base/net_transmit.h>
#include "trans_analyzer.h"
#include "score_war3msg.h"
#include "score_war3battle.h"

namespace JoyServer
{
namespace ScoreWar3
{
;
class CAnalyzerContext
{
public:
    CJoyTransContext*   m_transCtx;

    BYTE*   m_msgBuf;
    int     m_msgLen;

    void ReplaceCurrentMsg(const MessageBase* newMsg);
};

class CAnalyzer : public CJoyTransAnalyzer
{
public:
    typedef CMap<CJoyPathString, const CJoyPathString&, UINT, UINT> CValidMap;

    static CJoyCriticalSection  m_validMapLock;
    static CValidMap m_validMaps;

    CBattleMap  m_battles;
    UINT    m_validScoreDelay;

    // the valid battle finished from starting.
    UINT    m_validBattleCount;

    // chat messages.
    CJoyTransmitData*   m_chatmsg_gameTimeLack;

    void LogTransMsg(CAnalyzerContext& ctx, const MessageBase* msg);
public:

#   define JOY_WAR3_MSG_DEF(name, value) virtual void OnMsg_##name(CAnalyzerContext&, const MSG_##name*);
#   include "score_war3msgdef.h"

    template<typename MsgType>
    void DispatchMessage(CAnalyzerContext&, const MsgType* msg, void (CAnalyzer::*pHandler)(CAnalyzerContext&, const MsgType*));

    void OnTransmit(CJoyTransContext& ctx);
public:
    CAnalyzer();
    virtual ~CAnalyzer();

    virtual bool Create(CJoyMainRoom* pRoom);
    virtual void Destroy();

    UINT GetMapId(PCSTR mapPath);
	bool AddMapInfo(CString mapNameValue);
    void UserLeaveBattle(CJoyMainUser* pUser);

    void AddBattle(CBattle* pBattle);
    void QueueLog(int nLevel, PCSTR pszText, ...);
};


} // namespace ScoreWar3
} // namespace JoyServer

#endif  // __SCORE_WAR3ANALYZER_H_SERVER_SVR_MAIN__